﻿#include "mylistwidgetitem.h"
#include "ui_mylistwidgetitem.h"
#include "userinfowidget.h"
#include "verifywidget.h"
#include "chatsocket.h"

MyListWidgetItem::MyListWidgetItem(QWidget *parent,int type) :
    QWidget(parent),
    ui(new Ui::MyListWidgetItem)
{
    ui->setupUi(this);
    if(type == 0)
        ui->btn_add->setText("加好友");
    else if(type == 1)
        ui->btn_add->setText("加群聊");
}

MyListWidgetItem::~MyListWidgetItem()
{
    delete ui;
}

void MyListWidgetItem::setStyle(const QString& style,const UserInfo& userInfo)
{
    ui->label->setText(userInfo.nickname);
    ui->label->adjustSize();
    ui->btn_head->setStyleSheet(style);
    m_map[userInfo.id] = userInfo;
    m_id = userInfo.id;
}

void MyListWidgetItem::setStyle(const QString &style, const GroupInfo &gInfo)
{
    ui->label->setText(gInfo.nickname);
    ui->label->adjustSize();
    ui->btn_head->setStyleSheet(style);
    m_map2[gInfo.id] = gInfo;
    m_id = gInfo.id;
}

void MyListWidgetItem::setBtnText(const QString& text)
{
    ui->btn_add->setText(text);
}

void MyListWidgetItem::on_btn_head_clicked()//弹出个人信息
{
    if(ui->btn_add->text()=="加群聊")return;//暂时不处理群信息
    UserInfoWidget* uw = new UserInfoWidget;
    uw->initInfo(m_map[m_id],false);
    if(ui->btn_add->text()=="加好友")
        uw->setBtnText("加好友");
    else if(ui->btn_add->text()=="发消息")
        uw->setBtnText("发消息");
    uw->show();
}

void MyListWidgetItem::on_btn_add_clicked()//发送好友请求
{
    if(ui->btn_add->text()=="加群聊")return;//暂时不处理群信息
    if(ui->btn_add->text()=="查看资料")
    {
        UserInfoWidget* uw = new UserInfoWidget;
        uw->initInfo(m_map[m_id],false);
        uw->show();
        return;
    }
    if(ui->btn_add->text()=="发消息")
    {
        //若原来有此聊天窗口则将其显示
        for(auto obj : ChatSocket::getInstance()->m_chatList)
        {
            QString str = obj->getWinName();
            if(str.contains(m_map[m_id].id))
            {
                obj->raise();
                obj->show();
                return;
            }
        }
        //否则创建
        ChatWidget* cw = new ChatWidget;
        cw->setWinName(m_map[m_id].nickname+"("+m_map[m_id].id+")");
        cw->show();
        ChatSocket::getInstance()->m_chatList.append(cw);
        //查找有无与其的聊天记录
        //查找有无聊天记录
        QString str(m_map[m_id].nickname+"("+m_map[m_id].id+")");
        int i = str.indexOf('(');
        str =str.replace(")","");
        str = str.mid(i+1);
        QString path = QCoreApplication::applicationDirPath();
        QString uid = Widget::getInstance()->getID();
        QString filename = QString("%1/%2/%3-%4.txt").arg(path).arg(uid).arg(uid).arg(str);
        QFile file(filename);
        if(file.exists())
        {
            if(!file.open(QIODevice::ReadOnly))
            {
                QMessageBox::warning(this,"警告","打开文件失败");
                return;
            }
            char info[1024];
            char info2[1024];
            while(!file.atEnd())
            {
                memset(info,0,1024);
                memset(info2,0,1024);
                file.readLine(info,1024);
                QString msg2="";
                while(1)
                {
                    file.readLine(info2,1024);
                    msg2 += QString(info2);
                    QString content(info2);
                    if(content.endsWith("わ\r\n"))
                    {
                        break;
                    }
                }
                QString msg = QString(info)+"&" + msg2;
                cw->restoreChatLog(msg);
            }
            file.close();
        }
        return;
    }
    VerifyWidget* vw = new VerifyWidget;
    vw->initAllInfo(m_map[m_id]);
    vw->show();
}
